// JavaScript Document

// The return callback to read user from session and post to sidebar element
function ReadUser(user)
{
	if (user == null || user == "")
	{
		window.location.replace("../index.html");
	}
	else
	{
		sessionStorage.user = user;
		$("#username").append("\"" + sessionStorage.user + "\"");
	}
}

// Read Photo
function ReadPhoto()
{
	$.get("../SessionReader?attrname=photosrc", function(data, status)
	{
		if (data.trim() != "null")
		{
			var photoSrc = "../resources/userphotos/" + data;
			sessionStorage.photoSrc = photoSrc;
			var width = $("#userphoto").innerWidth();
			var img = "<img src=\"" + photoSrc + "\" width=\"" + width + "\" />";
			$("#userphoto").html(img);
		}
	});
}

// AJAX get the user from the session
function CheckUser()
{
	$.get("../SessionReader?attrname=user", ReadUser);
	$.get("../SessionReader?attrname=name", function(data)
	{
		sessionStorage.name = data;
	});
};

function CreateElement(indent, element, content)
{
	var td = "";
	for (var index = 0; index < indent; index += 1)
	{
		td += "\t";
	}
	td += "<" + element + ">\n";
	td += content + "\n";
	for (var index = 0; index < indent; index += 1)
	{
		td += "\t";
	}
	td += "</" + element + ">";
	
	return td;
}

function AppendContent(containerId, content)
{
	$("#" + containerId).append(content);
}

// -- AJAX Load Page Functions
// Load the profile page into the main element
function LoadProfile()
{
	$("#content_holder").load("profile.html", function()
	{
		FillPageCSS();
		$("#profile_header").css({"text-align" : "left"});
		LoadUserProfile();
		$("#kml_download_options_button, #kml_show_options_button").hide("slow");
	});
}

// Load the map page into the main element
function LoadMap()
{
	$("#content_holder").load("map.html", function()
	{
		FillPageCSS();
		LoadMapPage();
		$("#kml_show_options_button, #kml_download_options_button").show("slow");
		MapInitialise();
	});
}

// Load the about page into the main element
function LoadAbout()
{
	$("#content_holder").load("about.html", function()
	{
		FillPageCSS();
		$("#kml_download_options_button, #kml_show_options_button").hide("slow");
	});
}

// Page Load Function
$("body").ready(function()
{
	CheckUser();
	ReadPhoto();
	LoadMap();
	var height = window.innerHeight;
	var width = window.innerWidth - 20;
	$("table").css("height", height + "px");
	$("table").css("width", width + "px");

	var sidebar = ["#profilelink", "#maplink", "#aboutlink", "#logoutlink",
	               "#kml_download_options_button", "#kml_show_options_button"];
	
	for (elem in sidebar)
	{
		$(sidebar[elem])
			.mousedown(function(e) 
			{
				OnMouseDown(this.id);
			})
			.mouseup(function(e)
			{
				OnMouseUp(this.id);
			})
			.mouseout(function(e)
			{
				OnMouseOut(this.id);
			});
	}
		
	$("#logoutlink").mouseup(function()
	{
		OnMouseOut("logoutlink");
		sessionStorage.user = null;
		sessionStorage.name = null;
		sessionStorage.photoSrc = null;
		$.post("../SessionReader?action=clear");
		setTimeout(function()
		{
			window.location.replace("../index.html");
		}, 1000);
	});
	
	setTimeout(function()
	{
		if ((sessionStorage.length == 0) || (sessionStorage.user == "null"))
		{
			$("#logoutlink").mouseup();
		}
	},1500);
});
